package com.antoco.operation.bigscreen.service.autogeneration.impl;

import com.antoco.operation.bigscreen.repository.SuiweiWatchRepository;
import com.antoco.operation.bigscreen.model.entity.SuiweiWatch;
import com.antoco.operation.bigscreen.model.entity.TjIns;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SuiweiWatchService;
import com.antoco.operation.bigscreen.utils.BeanUtils;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class SuiweiWatchServiceImpl extends AbstractModelServiceImpl implements SuiweiWatchService {
    private final SuiweiWatchRepository repository;

    public SuiweiWatchServiceImpl(SuiweiWatchRepository repository) {
        this.repository = repository;
    }

    @Override
    public SuiweiWatch save(SuiweiWatch entity) {
        TjIns tjIns = BeanUtils.saveTjIns(SuiweiWatch.class);
        entity.setTjInsId(tjIns.getId());
        return repository.save(entity);
    }

    @Override
    public List<SuiweiWatch> save(List<SuiweiWatch> entities) {
        TjIns tjIns = BeanUtils.saveTjIns(SuiweiWatch.class);
        for (SuiweiWatch suiweiWatch : entities) {
            suiweiWatch.setTjInsId(tjIns.getId());
        }
        return (List<SuiweiWatch>) repository.saveAll(entities);
    }

    @Override
    public void deleteById(String id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<SuiweiWatch> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public List<SuiweiWatch> findAll() {
        return (List<SuiweiWatch>) repository.findAll();
    }

    @Override
    public Page<SuiweiWatch> findAll(Pageable pageable) {
        Page<SuiweiWatch> entityPage = repository.findAll(pageable);
        List<SuiweiWatch> entities = entityPage.getContent();
        return new PageImpl<>(entities, pageable, entityPage.getTotalElements());
    }

    @Override
    public SuiweiWatch update(SuiweiWatch entity, String id) {
        Optional<SuiweiWatch> optional = findById(id);
        if (optional.isPresent()) {
            return save(entity);
        }
        return null;
    }
}